package com.liwenfeng.diancanspringboot.service.impl;

import com.liwenfeng.diancanspringboot.common.context.BaseContext;
import com.liwenfeng.diancanspringboot.common.exception.ShoppingCartBusinessException;
import com.liwenfeng.diancanspringboot.mapper.DishMapper;
import com.liwenfeng.diancanspringboot.mapper.SetmealMapper;
import com.liwenfeng.diancanspringboot.mapper.ShoppingCartMapper;
import com.liwenfeng.diancanspringboot.pojo.dto.ShoppingCartDTO;
import com.liwenfeng.diancanspringboot.pojo.dto.ShoppingCartSelectAllDto;
import com.liwenfeng.diancanspringboot.pojo.entity.ShoppingCart;
import com.liwenfeng.diancanspringboot.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setTurnoverId(Long.valueOf(Objects.requireNonNull(BaseContext.getOther())));
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list != null && list.size() > 0) {
            shoppingCartMapper.updateNumberById(list.get(0).getNumber() + shoppingCartDTO.getNumber(), list.get(0).getId());
        } else {
            shoppingCart.setNumber(shoppingCartDTO.getNumber());
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查询购物车列表，按turnoverId
     *
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart(Long turnoverId) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setTurnoverId(turnoverId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 清空购物车 todo 改成按trunoverId清空
     */
    @Override
    public void cleanShoppingCart(Long turnoverId) {
        if (turnoverId == 0) {
            shoppingCartMapper.cleanAll();
        }
        shoppingCartMapper.deleteByturnoverId(turnoverId);
    }

    /**
     * 减少购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = null;
        if (shoppingCartDTO.getId() != null) {
            shoppingCart = shoppingCartMapper.getById(shoppingCartDTO.getId());
            if (shoppingCart == null) {
                throw new ShoppingCartBusinessException("没找到该商品");
            }
        } else {
            shoppingCart = new ShoppingCart();

            BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
            //设置查询条件，查询当前登录用户的购物车数据

            shoppingCart.setTurnoverId(Long.valueOf(Objects.requireNonNull(BaseContext.getOther())));
            List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
            if (list == null || list.size() == 0) {
                throw new ShoppingCartBusinessException("没找到该商品");
            }
            shoppingCart = list.get(0);
        }

        Long id = shoppingCart.getId();

        Integer number = shoppingCart.getNumber();
        if (number > 1) {
            //当前商品在购物车中的份数不为1，修改份数即可
            shoppingCartMapper.updateNumberById(number - 1, id);
        } else {
            //当前商品在购物车中的份数为1，直接删除当前记录
            shoppingCartMapper.deleteById(id);
        }
    }

    @Override
    public void updateById(ShoppingCart shoppingCart) {
        if (shoppingCart.getNumber() != null && shoppingCart.getNumber() == 0) {
            shoppingCartMapper.deleteById(shoppingCart.getId());
        } else {
            shoppingCartMapper.updateById(shoppingCart);
        }
    }

    @Override
    public void updateByIds(ShoppingCartSelectAllDto shoppingCartSelectAllDto) {
        shoppingCartMapper.updateByIds(shoppingCartSelectAllDto.getIds(), shoppingCartSelectAllDto.getSelected());
    }
}
